<include file="/public/header" />
<style>
    body {
        position: absolute;
        overflow-y:scroll;
        background-color: #fff;
    }
    .layui-card-body {
        padding-left: 0;
    }

    .layui-form-item .layui-word-aux {
        padding-top: 6px!important;
    }
    .layui-form-item .layui-input-fullIP {
        width: 80%;
        word-break: break-word;
    }

    td,.del,input,select {
        font-size: 12px;
    }
    .del {
        cursor: pointer;
    }

    .del:hover {
        color: red;
    }

    .layui-elem-field legend {
        font-size: 16px;
    }

</style>
<php>
    $field_type = ['string'=>'字符型','numeric'=>'数字型','date'=>'日期型','id'=>'主键型','title'=>'标题型','body'=>'内容型',];
    $field_index = ['none'=>'不做索引','self'=>'字段索引','mixed'=>'混合区索引','both'=>'字段和混合区',];
    $field_tokenizer = ['default'=>'default','full'=>'full','none'=>'none','split'=>'split','xlen'=>'xlen','xstep'=>'xstep',];
</php>
<div class="layui-fluid">
    <form class="layui-form" action="{:url('/system.fulltext/fields')}">
    <gt name="$data.id" value="0"><input type="text" name="id" value="{$data.id}" hidden=""></gt>
    <fieldset class="layui-elem-field">
        <legend>项目信息</legend>
        <div class="layui-field-box">
            <div class="layui-form-item">
                <label class="layui-form-label">{:__('项目名称')}</label>
                <div class="layui-input-inline">
                    <div class="layui-form-mid layui-word-aux" >{$data.name}</div>
                </div>
                <label class="layui-form-label">{:__('服务器类型')}</label>
                <div class="layui-input-inline">
                    <div class="layui-form-mid layui-word-aux" >{$data.type}</div>
                </div>
            </div>
            <!-- // XunSearch -->
            <div class="layui-form-item">
                <label class="layui-form-label">{:__('索引服务器')}</label>
                <div class="layui-input-inline layui-input-fullIP">
                    <div class="layui-form-mid layui-word-aux" >{$data.index}</div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">{:__('搜索服务器')}</label>
                <div class="layui-input-inline layui-input-fullIP">
                    <div class="layui-form-mid layui-word-aux">{$data.search}</div>
                </div>
            </div>
        </div>
      </fieldset>
      <fieldset class="layui-elem-field" style="margin-top: 10px;">
        <legend>字段集设计</legend>
        <div class="layui-field-box">
            <table class="layui-table" lay-skin="nob">
                <thead>
                  <tr>
                    <th>名称</th>
                    <th>类型</th>
                    <th width="120">索引</th>
                    <th>分词器</th>
                    <th>权重</th>
                    <th>精确搜索</th>
                    <th width="30"></th>
                  </tr> 
                </thead>
                <tbody>
                    <empty name="$data['field']">
                    <tr>
                        <td><input name="field_name[]" type="text" class="layui-input"></td>
                        <td>
                            <select name="field_type[]" lay-filter="selectField">
                                <volist name="field_type" id="vo">
                                    <option value="{$key}">{$vo}</option>
                                </volist>
                            </select>
                        </td>
                        <td><select name="field_index[]">
                            <volist name="field_index" id="vo">
                                <option value="{$key}">{$vo}</option>
                            </volist>
                        </select>
                        </td>
                        <td><select name="field_tokenizer[]">
                            <volist name="field_tokenizer" id="vo">
                                <option value="{$key}">{$vo}</option>
                            </volist>
                        </select>
                        </td>
                        <td><input name="field_weight[]" type="text" class="layui-input" value="1"></td>
                        <td><select name="field_phrase[]">
                            <option value="0">否</option>
                            <option value="1">是</option>
                        </select>
                        </td>
                        <td><span class="del">删除</span></td>
                    </tr>
                    <else/>
                        <!-- // 编辑状态 -->
                        <volist name="$data['field']" id="vv" >
                            <tr>
                                <td><input name="field_name[]" type="text" class="layui-input" value="{$key}"></td>
                                <td>
                                    <select name="field_type[]" lay-filter="selectField">
                                        <volist name="field_type" id="vo">
                                            <option value="{$key}" <if ($key == $vv['type'])> selected </if>>{$vo}</option>
                                        </volist>
                                    </select>
                                </td>
                                <td><select name="field_index[]">
                                    <volist name="field_index" id="vo">
                                        <option value="{$key}" value="{$key}" <if (isset($vv['index']) AND $key == $vv['index'])> selected </if> >{$vo}</option>
                                    </volist>
                                </select>
                                </td>
                                <td><select name="field_tokenizer[]">
                                    <volist name="field_tokenizer" id="vo">
                                        <option value="{$key}" value="{$key}" <if (isset($vv['tokenizer']) AND $key == $vv['tokenizer'])> selected </if> >{$vo}</option>
                                    </volist>
                                </select>
                                </td>
                                <td><input name="field_weight[]" type="text" class="layui-input" value="<if (isset($vv['weight']))>{$vv.weight}<else/>1</if>"></td>
                                <td><select name="field_phrase[]">
                                    <option value="0">否</option>
                                    <option value="1" <if (isset($vv['phrase']) AND $vv['phrase'] == 1)> selected </if> >是</option>
                                </select>
                                </td>
                                <td><span class="del">删除</span></td>
                            </tr>
                        </volist>
                    </empty>
                </tbody>
            </table>
            <div style="margin-left: 15px;">
                <button type="button" class="layui-btn layui-btn-xs layui-btn-add">添加</button>
                <button type="reset" class="layui-btn layui-btn-xs layui-btn-danger">重置</button>
                <button class="layui-btn layui-btn-xs" lay-submit="" lay-filter="submitIframe" type="button">立即提交</button>
            </div>
        </div>
      </fieldset>

    </form>
</div>
<include file="/public/footer" />

<script>
    layui.use(['form'], function () {

        var form = layui.form;
        var jquery = layui.jquery;

        // 添加元素
        jquery('.layui-btn-add').click(function(elem) {
            var tr = jquery('tbody').find('tr:first-child').html();
            tr = tr.replace(/selected=""/g,'');
            tr = tr.replace(/<input(.*?)value="(\w+)"/g,'<input$1');
            jquery('tbody').append('<tr>'+tr+'</tr>');
            
            /* 重置BUG
            var tr = jquery('tbody').find('tr:last-child');
            tr.each(function(n,v) {
                jquery(v).find('input,select').val('');
            })*/
            form.render();
        })

        // 删除元素
        jquery('body').on('click','.del',function(){
            var tr = jquery(this).parents('tr');
            if (tr.index() >= 1) {
                tr.remove();
            }
        })

        form.on('select(selectField)',function(data) {
            var val = data.value, type = {
                'id': "self",
                'title': "both",
                'body': "mixed",
            }
            for (var i in type) {
                if (i == val) {
                    var rs = jquery(this).parents('td').next('td');
                    rs.children('select').val(type[i]);
                    form.render();
                    break;
                }
            }
        })
    })
</script>
